package com.kun.companion.core.ui.view;

import com.kun.companion.core.queue.ui.button.AwaitEventButton;
import com.kun.companion.core.ui.list.BaseListCell;
import com.kun.companion.core.ui.list.BaseListView;
import com.kun.companion.core.util.ComUtil;
import com.kun.companion.core.util.ImageLoader;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.shape.SVGPath;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * TODO
 *
 * @author gzc
 * @since 2025/2/9 20:12
 **/
public class CollapseListViewPane extends BorderPane {

    protected double maxWidth = 150;
    protected double minWidth = 70;

    protected final CollapseListView listView;

    protected AwaitEventButton button;

    protected static BorderPane contentPane;

    protected static final Map<String, Node> cacheMap = new ConcurrentHashMap<>(16);
    /**
     * 是否折叠
     */
    protected final SimpleBooleanProperty collapseProperty = new SimpleBooleanProperty(false);

    public CollapseListViewPane() {
        this(new BorderPane());
    }

    public void setContentPane(BorderPane contentPane1) {
        contentPane = contentPane1;
    }

    public BorderPane getContentPane() {
        return contentPane;
    }

    public CollapseListViewPane(BorderPane contentPane1) {
        super();
        this.listView = new CollapseListView(collapseProperty);
        contentPane = contentPane1;
        getStyleClass().add("collapse-list-view-pane");
        setMaxSize(maxWidth, Integer.MAX_VALUE);
        setTop(createTop());
        setCenter(listView);
    }

    private Node createTop() {
        ImageView imageView = ImageLoader.loadView("/app-icon.png", 20, 20);
        button = new AwaitEventButton("展叠");
        button.action(this::collapse);
        return new HBox(2, imageView, button);
    }

    public void addItem(Collapse item) {
        listView.getItems().add(item);
    }


    public void collapse() {
        ComUtil.uiThreadRun(() -> {
            collapseProperty.set(!collapseProperty.get());
            if (collapseProperty.get()) {
                loadCollapse();
            } else {
                logExpanded();
            }
            // 强制刷新所有单元格
            listView.refresh();
            requestLayout();
        });
    }

    private void logExpanded() {
        ImageView imageView = ImageLoader.loadView("/app-icon.png", 20, 20);
        button = new AwaitEventButton("展叠");
        button.action(this::collapse);
        HBox hBox = new HBox(2, imageView, button);
        setTop(hBox);
        setMaxWidth(maxWidth);
        listView.setMaxWidth(maxWidth);
        listView.setBackground(new Background(new BackgroundFill(Paint.valueOf("#9c8164"), null, null)));
    }

    private void loadCollapse() {
        SVGPath svgGraphic = new SVGPath();
        svgGraphic.setContent("M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5");
        button = new AwaitEventButton(null, svgGraphic, "展开");
        button.action(this::collapse);
        setTop(button);
        setMaxWidth(minWidth);
        listView.setMaxWidth(minWidth);
        listView.setBackground(new Background(new BackgroundFill(Paint.valueOf("#adc48e"), null, null)));
    }


    public static class CollapseListView extends BaseListView<Collapse> {
        public CollapseListView(SimpleBooleanProperty collapseProperty) {
            super(lc -> new CollapseListCell(collapseProperty));
        }

    }

    public static class CollapseListCell extends BaseListCell<Collapse> {
        private final SimpleBooleanProperty collapseProperty;

        public CollapseListCell(SimpleBooleanProperty collapseProperty) {
            super();
            this.collapseProperty = collapseProperty;
        }

        @Override
        protected void initStyle() {
            super.initStyle();
        }

        @Override
        protected void updateLayout() {
            super.updateLayout();
            // 确保按钮占据整个单元格
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        @Override
        protected void bindData(Collapse item) {
//            Node node = cacheMap.get(item.getCellText());
//            if (node == null) {
//                node = new StackPane();
//                cacheMap.put(item.getCellText(), node);
//            }
            SVGPath svgPath = new SVGPath();
            svgPath.setContent("M5.5.5a.5.5 0 0 0-1 0V2A2.5 2.5 0 0 0 2 4.5H.5a.5.5 0 0 0 0 1H2v1H.5a.5.5 0 0 0 0 1H2v1H.5a.5.5 0 0 0 0 1H2v1H.5a.5.5 0 0 0 0 1H2A2.5 2.5 0 0 0 4.5 14v1.5a.5.5 0 0 0 1 0V14h1v1.5a.5.5 0 0 0 1 0V14h1v1.5a.5.5 0 0 0 1 0V14h1v1.5a.5.5 0 0 0 1 0V14a2.5 2.5 0 0 0 2.5-2.5h1.5a.5.5 0 0 0 0-1H14v-1h1.5a.5.5 0 0 0 0-1H14v-1h1.5a.5.5 0 0 0 0-1H14v-1h1.5a.5.5 0 0 0 0-1H14A2.5 2.5 0 0 0 11.5 2V.5a.5.5 0 0 0-1 0V2h-1V.5a.5.5 0 0 0-1 0V2h-1V.5a.5.5 0 0 0-1 0V2h-1V.5zm1 4.5h3A1.5 1.5 0 0 1 11 6.5v3A1.5 1.5 0 0 1 9.5 11h-3A1.5 1.5 0 0 1 5 9.5v-3A1.5 1.5 0 0 1 6.5 5z");
            AwaitEventButton button = new AwaitEventButton(item.getCellText(), svgPath, "啊啊啊");
            button.action(() -> {
                ComUtil.uiThreadRun(()->{
                    Function<Node, Node> function = item.getFunction();
                    Node node = cacheMap.get(item.getCellText());
                    Node apply = function.apply(node);
                    cacheMap.put(item.getCellText(), apply);
                    contentPane.setCenter(apply);
                });
            });
//            button.setMaxWidth(Integer.MAX_VALUE);
//            button.setAlignment(Pos.CENTER);
            // 折叠
            if (collapseProperty.get()) {
                button.withText(null);
                setGraphic(button);
            }
            // 展开
            else {
                button.withText(item.cellText);
                setGraphic(button);
//                HBox container = new HBox(imageView, new Label(item.cellText));
//                container.setBorder(new Border(new BorderStroke(Paint.valueOf("#9c8164"), BorderStrokeStyle.SOLID, null, new BorderWidths(2))));
//                setGraphic(container);
            }

        }

    }

    public static class Collapse {
        private String imgPath;
        private String cellText;
        private Runnable runnable;
        private Function<Node, Node> function;

        public Collapse(String imgPath, String cellText) {
            this(imgPath, cellText, () -> {
            });
        }

        public Collapse(String imgPath, String cellText, Runnable runnable) {
            this.imgPath = imgPath;
            this.cellText = cellText;
            this.runnable = runnable;
        }

        public String getImgPath() {
            return imgPath;
        }

        public void setImgPath(String imgPath) {
            this.imgPath = imgPath;
        }

        public String getCellText() {
            return cellText;
        }

        public void setCellText(String cellText) {
            this.cellText = cellText;
        }

        public Runnable getRunnable() {
            return runnable;
        }

        public void setRunnable(Runnable runnable) {
            this.runnable = runnable;
        }

        public Function<Node, Node> getFunction() {
            return function;
        }

        public void setFunction(Function<Node, Node> function) {
            this.function = function;
        }
    }

}

